package com.start.form.engine.core.rule;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.start.common.exceptions.StartBusException;
import com.start.form.engine.core.StartDataAction;
import com.start.form.engine.core.model.StartFormColumnModel;
import org.apache.commons.lang3.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class DataRuleVerification {
    /**
     * 数据验证
     *
     * @param column
     * @param data
     * @param action
     */
    public static void verification(StartFormColumnModel column, String data, StartDataAction action) {
        if (StringUtils.isEmpty(column.getColumnOption())) return;
        JSONObject object = JSONObject.parseObject(column.getColumnOption());
        if (!object.containsKey("rules")) return;
        JSONArray rules = object.getJSONArray("rules");
        List<JSONObject> rulesList = rules.stream().map(e -> (JSONObject) e).sorted(Comparator.comparing(e -> e.containsKey("required") ? -1 : 0)).collect(Collectors.toList());
        boolean required = object.containsKey("required") ? object.getBoolean("required") : false;
        if (!required && StringUtils.isEmpty(data)) return;

        if (required && !StringUtils.isEmpty(data) && rulesList.size() < 1) return;

        if (required && StringUtils.isEmpty(data)) {
            JSONObject jsonObject = getRule("required", column.getLabel() + "不能为空！", rulesList);
            if (jsonObject != null) throw new StartBusException(jsonObject.getString("message"));
        }
        if (!object.containsKey("pattern") || StringUtils.isEmpty(object.getString("pattern"))) return;
        String patternStr = object.getString("pattern");
        int lastIndexOf = patternStr.lastIndexOf("/g");
        if (patternStr.endsWith("/g")) {
            patternStr = patternStr.substring(0, lastIndexOf);
        }
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(data);
        if (!matcher.matches()) {
            JSONObject jsonObject = getRule("pattern", column.getLabel() + "格式不正确！", rulesList);
            if (jsonObject != null) throw new StartBusException(jsonObject.getString("message"));
        }

    }

    /**
     * 查找指定数据规则
     *
     * @param key
     * @param defaultMessage
     * @param rulesList
     * @return
     */
    private static JSONObject getRule(String key, String defaultMessage, List<JSONObject> rulesList) {
        if (rulesList == null || rulesList.size() < 1) return null;
        if (StringUtils.isEmpty(key)) return null;
        List<JSONObject> collect = rulesList.stream().filter(e -> e.containsKey(key)).limit(1).collect(Collectors.toList());
        if (collect.size() < 1) return null;
        JSONObject object = collect.get(0);
        if (!object.containsKey("message")) {
            object.put("message", defaultMessage);
        }
        if (StringUtils.isEmpty(object.getString("message"))) {
            object.remove("message");
            object.put("message", defaultMessage);
        }
        return object;
    }
}
